package com.lanqiao.companymanage.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanqiao.companymanage.entity.AssetInformation;
import com.lanqiao.companymanage.mapper.AssetMessMapper;
import com.lanqiao.companymanage.service.AssetMessService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 何小棍
 * 资产信息管理与添加
 */
@Service
public class AssetMessServiceImpl extends ServiceImpl<AssetMessMapper, AssetInformation> implements AssetMessService {
    /**
     * 分页查询
     */
    @Override
    public Page<AssetInformation> listPage(Page<AssetInformation> assetMessPage) {
        return baseMapper.selectPageAssetMess(assetMessPage);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public AssetInformation selectById(Integer id) {
        return baseMapper.selectByIdAssetMess(id);
    }

    /**
     * 资产信息管理
     * @param assetMess
     * @return
     */
    @Override
    @Transactional
    public int addAssetMess(AssetInformation assetMess) {
        // 先添加资产
        int insert = baseMapper.insert(assetMess);
        // 再插入关联表
        baseMapper.insertAssetTea(assetMess.getId(),assetMess.getAssetClass().getId());
        return insert;
    }

    /**
     * 根据id删除
     */
    @Override
    @Transactional
    public int deleteById(Integer id) {
        int deleteById = baseMapper.deleteById(id);
        baseMapper.deleteAssetTea(id);
        return deleteById;
    }

    /**
     * 根据id修改
     */
    @Override
    @Transactional
    public int updateAssetMess(AssetInformation assetMess) {
        int updateById = baseMapper.updateById(assetMess);
        //先删除关联表数据库,再插入关联表数据
        baseMapper.deleteAssetTea(assetMess.getId());
        baseMapper.insertAssetTea(assetMess.getId(),assetMess.getAssetClass().getId());
        return updateById;
    }
}
